﻿@page "/Courses/CoursePage"
@layout ChapterLayout
@inject CourseService CourseService
@inject CourseStateService NavState
@inject NavigationManager Navigation
@inject AuthenticationStateProvider AuthStateProvider
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@attribute [Authorize]
@using Microsoft.AspNetCore.Authorization
@using AntDesign
@using lcwb.Shared.Service
@using lcwb.Shared.Model
@using lcwb.Pages.Course.Components

<div>
    <Title Level="1" Style="text-align:center">@Course?.CourseName</Title>
    <Paragraph Style="text-align : left">
        @((MarkupString)CourseInfoMarkdown)
    </Paragraph>
    <Title Level="1">教学视频</Title>
   @*
       <VideoCell VideoUrl="@courseVideoURL"></VideoCell>
   *@
</div>



@code {
    // Configure the pipeline with all advanced extensions active


    private Course Course { get; set; } = new Course();
    private string courseVideoURL = "https://www.bilibili.com/video/BV1XZ4y1S7e1";
    private string CourseInfoMarkdown = "";
    //private string MarkdownHtml = Markdig

    protected override async Task OnInitializedAsync()
    {

        NavState.OnStateChange += StateChanged;
        await LoadCourseData();
    }

    private void StateChanged()
    {
        // 状态变化时刷新数据
        // ComponentBase
        StateHasChanged();
        // 异步调用，注意不使用同步等待
        _ = LoadCourseData();
    }

    private async Task LoadCourseData()
    {
        try
        {
            if (NavState.CurrentCourseId.HasValue)
            {
                Course = await CourseService.GetCourseByIdAsync(NavState.CurrentCourseId.Value);
                var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
                if (Course.Content is not null)
                {
                    CourseInfoMarkdown = Markdig.Markdown.ToHtml(Course.Content, pipeline);
                }
                
            }
            else
            {
                Navigation.NavigateTo("/NoCourseIdPassed");
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine($"课程加载失败: {ex.Message}");
            Navigation.NavigateTo("/404");
        }
    }
    public void Dispose()
    {
        NavState.OnStateChange -= StateChanged;
    }
}